package org.db2.project.airline.service;

import org.db2.project.airline.dao.DAOException;
import org.db2.project.airline.dao.FlightPlanDAO;
import org.db2.project.airline.dao.impl.HibernateFlightPlanDAO;
import org.db2.project.airline.model.Airport;
import org.db2.project.airline.model.FlightClassType;
import org.db2.project.airline.model.FlightPlan;
import org.db2.project.airline.utils.HibernateHelper;

import java.util.Date;
import java.util.List;

/**
 *
 */
public class FlightPlanService {

    private final FlightPlanDAO flightPlanDAO = new HibernateFlightPlanDAO();

    public FlightPlanService() {
        // empty constructor
    }

    public List<FlightPlan> findPlan(Airport departureAirport, Airport arrivalAirport,
                                     FlightClassType flightClassType, Date departureTime, Date arrivalTime, boolean twoDaysSpan) throws DAOException {

        try {
            HibernateHelper.beginTransaction();

            List<FlightPlan> flightPlans = flightPlanDAO.find(departureAirport, arrivalAirport,
                    flightClassType, departureTime, arrivalTime, twoDaysSpan);

            HibernateHelper.commitTransaction();

            return flightPlans;
        } catch (DAOException e) {
            HibernateHelper.rollbackTransaction();

            throw e;
        }
    }
}
